package com.gongpingjialoan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.CarListActivity;
import com.gongpingjialoan.bean.CarSource;
import com.gongpingjialoan.bean.VipBuycarBean;
import com.gongpingjialoan.utility.Utils;

import java.util.List;

/**
 * Created by Administrator on 2016/3/22.
 */
public class VipCarListAdapter extends BaseAdapter {


    private LayoutInflater mInflater;

    private Context context;

    public List<CarSource> mCarSourceList;

    private VipBuycarBean mVipBuycarBean;


    public VipCarListAdapter(Context context, VipBuycarBean bean) {
        this.context = context;
        this.mVipBuycarBean = bean;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<CarSource> CarSourceList) {

        this.mCarSourceList = CarSourceList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mCarSourceList == null) {
            return 0;
        }
        return mCarSourceList.size();
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final CarSourceViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.vip_carlist_item, null);
            holder = new CarSourceViewHolder();
            holder.brandname = (TextView) convertView.findViewById(R.id.brand_name);
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.car_numTextView = (TextView) convertView.findViewById(R.id.car_num);
            holder.renzhengImageView = (ImageView) convertView.findViewById(R.id.renzheng);
            holder.tvCarTitle = (TextView) convertView.findViewById(R.id.carName);
            holder.pushTextView = (TextView) convertView.findViewById(R.id.push_time);
            holder.tvCarPrice = (TextView) convertView.findViewById(R.id.price);
            holder.tvGPJIndex = (TextView) convertView.findViewById(R.id.tv_index);
            holder.tvDomain = (TextView) convertView.findViewById(R.id.domain);
            holder.tuihuanImageView = (TextView) convertView.findViewById(R.id.tuihuan_img);
            holder.shiguImageView = (TextView) convertView.findViewById(R.id.shigu_img);
            holder.index_img = (ImageView) convertView.findViewById(R.id.index_img);
            holder.index_txt = (TextView) convertView.findViewById(R.id.index_txt);
            holder.timeTextView = (TextView) convertView.findViewById(R.id.time);
            holder.shangImageView = (TextView) convertView.findViewById(R.id.shang_id);
            holder.zhibaoImageView = (ImageView) convertView.findViewById(R.id.zhibao_view);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (CarSourceViewHolder) convertView.getTag(R.id.tag_first);
        }
        CarSource source = mCarSourceList.get(position);
        convertView.setTag(R.id.tag_second, source);


        int carNum = source.car_num;
        String title;
        if (carNum > 100) {
            title = "他家共有 100+ 辆车适合您";
            holder.car_numTextView.setText(Utils.changeColor(context, title, 4, 10, R.color.new_color));
        } else {
            title = "他家共有 " + carNum + " 辆车适合您";
            int size = (carNum + "").length();
            holder.car_numTextView.setText(Utils.changeColor(context, title, 5, 5 + size, R.color.new_color));
        }
        holder.car_numTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CarListActivity.class);
                intent.putExtra("type", "vip_domain");
                intent.putExtra("mVipBuycarBean", mVipBuycarBean);
                intent.putExtra("domain", mCarSourceList.get(position).mSource);
                context.startActivity(intent);
            }
        });


        if (source.is_certify) {
            if ("dealer".equals(source.dealer_category)) {
                holder.renzhengImageView.setImageResource(R.drawable.renzheng);
            } else if ("4s".equals(source.dealer_category)) {
                holder.renzhengImageView.setImageResource(R.drawable.renzheng_4s);
            } else {
                holder.renzhengImageView.setImageResource(R.drawable.trans);
            }
            holder.renzhengImageView.setVisibility(View.VISIBLE);
        } else {
            holder.renzhengImageView.setVisibility(View.GONE);
        }

        if (source.mQsTags.contains("质保")) {
            holder.zhibaoImageView.setVisibility(View.VISIBLE);
            holder.shangImageView.setVisibility(View.GONE);
        } else {
            holder.zhibaoImageView.setVisibility(View.GONE);
            holder.shangImageView.setVisibility(View.VISIBLE);
            if ("personal".equals(source.mSourceType)) {
                holder.shangImageView.setText("个");
                holder.shangImageView.setBackgroundResource(R.drawable.sort_ge);
                holder.shangImageView.setTextColor(context.getResources().getColor(R.color.white));
            } else if("dealer".equals(source.mSourceType) || "cpo".equals(source.mSourceType)|| "odealer".equals(source.mSourceType)){
                holder.shangImageView.setText("商");
                holder.shangImageView.setBackgroundResource(R.drawable.sort_shang);
                holder.shangImageView.setTextColor(context.getResources().getColor(R.color.white));
            }else{
                holder.shangImageView.setVisibility(View.GONE);
            }
        }

        holder.brandname.setText(source.model_zh);
        holder.timeTextView.setText(source.getmYear() + "年上牌/" + source.getmMile() + "万公里");
        if ("".equals(source.model_detail_zh)) {
            holder.tvCarTitle.setText(source.getmTitle());
        } else {
            holder.tvCarTitle.setText(source.model_detail_zh);
        }
        holder.tvCarPrice.setText(String.valueOf(source.mPrice));
        holder.tvGPJIndex.setText(String.valueOf(source.mGpjIndex));


        holder.tvGPJIndex.setVisibility(View.VISIBLE);
        holder.index_txt.setVisibility(View.VISIBLE);
        holder.index_img.setVisibility(View.VISIBLE);
        double index = source.mGpjIndex;
        if (index < 7) {
            holder.tvGPJIndex.setTextColor(context.getResources().getColor(R.color.bad_color));
            holder.index_txt.setText("不推荐");
            holder.index_txt.setTextColor(context.getResources().getColor(R.color.bad_color));
            holder.index_txt.setBackgroundResource(R.drawable.bad_bg);
            holder.index_img.setImageResource(R.drawable.bad_img);
            holder.index_txt.setVisibility(View.GONE);
        } else if (index < 9 && index >= 7) {
            holder.tvGPJIndex.setTextColor(context.getResources().getColor(R.color.yiban_color));
            holder.index_txt.setText("一般");
            holder.index_txt.setTextColor(context.getResources().getColor(R.color.yiban_color));
            holder.index_txt.setBackgroundResource(R.drawable.yiban_bg);
            holder.index_img.setImageResource(R.drawable.yiban_img);
            holder.index_txt.setVisibility(View.GONE);
        } else if (index >= 9) {
            holder.tvGPJIndex.setTextColor(context.getResources().getColor(R.color.tuijian_color));
            holder.index_txt.setText("超值");
            holder.index_txt.setTextColor(context.getResources().getColor(R.color.tuijian_color));
            holder.index_txt.setBackgroundResource(R.drawable.tuijian_bg);
            holder.index_img.setImageResource(R.drawable.tuijian_img);
            holder.index_txt.setVisibility(View.VISIBLE);
        } else if (index <= 0) {
            holder.tvGPJIndex.setVisibility(View.GONE);
            holder.index_txt.setVisibility(View.GONE);
            holder.index_img.setVisibility(View.GONE);
        }

        if (source.mQsTags.contains("可退换")) {
            holder.tuihuanImageView.setVisibility(View.VISIBLE);
        } else {
            holder.tuihuanImageView.setVisibility(View.GONE);
        }

        if (source.mQsTags.contains("无大事故")) {
            holder.shiguImageView.setVisibility(View.VISIBLE);
        } else {
            holder.shiguImageView.setVisibility(View.GONE);
        }

        holder.tvDomain.setText(source.mSource);

        String timetitle = Utils.getTimeByTimestamp(source.pub_timestamp, System.currentTimeMillis());
        holder.pushTextView.setText(timetitle);

        String uri = source.mUrl + "?imageMogr2/thumbnail/!50p";
        Glide.with(context).load(uri).placeholder(R.drawable.car_loading).centerCrop().crossFade().into(holder.imgThumbnail);

        return convertView;

    }

    public class CarSourceViewHolder {
        public ImageView imgThumbnail;

        public ImageView renzhengImageView;

        public TextView brandname;

        public TextView tvCarTitle;

        public TextView shangImageView;

        public ImageView zhibaoImageView;

        public TextView tvCarPrice;

        public TextView tvDomain;

        public TextView pushTextView;

        public TextView tvGPJIndex;

        public TextView tuihuanImageView;

        public TextView shiguImageView;

        public ImageView index_img;
        //
        public TextView car_numTextView;

        public TextView index_txt;

        public TextView timeTextView;

    }
}
